package com.pk.platform.business.privilege.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pk.platform.business.core.dao.impl.GenericDaoImpl;
import com.pk.platform.business.privilege.dao.IUserDao;
import com.pk.platform.business.privilege.vo.UserVO;
import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class UserDaoImpl extends GenericDaoImpl implements IUserDao {

	public List<Map<String, Object>> queryUser(UserVO uvo, Pager pager) {
		StringBuffer sql = new StringBuffer();
		sql.append("select u.*, d.deptName from p_user u left join p_department d on u.deptId = d.id where 1 = 1");
		List<Object> param = new ArrayList<Object>();
		if(uvo.getUser().getId() != null && !"".equals(uvo.getUser().getId()))
		{
			sql.append(" and u.id like ?");
			param.add("%" + uvo.getUser().getId() + "%");
		}
		if(uvo.getUser().getUsername() != null && !"".equals(uvo.getUser().getUsername()))
		{
			sql.append(" and u.username like ?");
			param.add("%" + uvo.getUser().getUsername() + "%");
		}
		if(uvo.getUser().getSex() != null && !"".equals(uvo.getUser().getSex()))
		{
			sql.append(" and u.sex = ?");
			param.add(uvo.getUser().getSex());
		}
		if(uvo.getUser().getDept() != null && uvo.getUser().getDept().getId() != null)
		{
			sql.append(" and u.deptId = ?");
			param.add(uvo.getUser().getDept().getId());
		}
		if(uvo.getUser().getUseType() != null && !"".equals(uvo.getUser().getUseType()))
		{
			sql.append(" and u.useType = ?");
			param.add(uvo.getUser().getUseType());
		}
		return this.getListByPage(sql.toString(), param.toArray(), pager);
	}
	
	public User login(String id, String password)
	{
		String hql = "from User u where u.id = ? and u.password = ? and u.useType = ?";
		List<User> list = this.getHibernateTemplate().find(hql, new Object[]{id, password, Constant.USE});
		if(list.size() > 0)
			return list.get(0);
		return null;
	}
	
	public List<Map<String, Object>> queryUserRoleIds(String userId)
	{
		String sql = "select * from p_userRoleRef where userId = ?";
		return this.getSjt().queryForList(sql, userId);
	}
	
	public List<Map<String, Object>> queryAllUser()
	{
		String sql = "select * from p_user";
		return this.getSjt().queryForList(sql);
	}
	
	public List<User> queryAllUserTest()
	{
		String hql = "from User u";
		return this.getHibernateTemplate().find(hql);
	}

}
